package cn.itzd.handler.exception;

import cn.itzd.utils.result.R;
import cn.itzd.utils.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * @Author: dada
 * @Version: 1.0
 * @Description:
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(SystemException.class)
    public R<?> systemExceptionHandler(SystemException e){
        //打印异常信息
        log.error("出现了异常: {}",e.getMessage());
        //从异常对象中获取提示信息封装返回
        return R.failed(e.getCode(),e.getMsg());
    }

    /**
     * 类型转换异常
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public R<?> exceptionNumberFormatHandler(Exception e){
        //打印异常信息
        log.error("出现了异常: {}",e.getMessage());
        //从异常对象中获取提示信息封装返回
        ResultCode rce = ResultCode.NUMBER_FORMAT_EXECUTION_ERROR;
        return R.failed(rce);
    }

    @ExceptionHandler(Exception.class)
    public R<?> exceptionHandler(Exception e){
        //打印异常信息
        log.error("出现了异常: {}",e.getMessage());
        //从异常对象中获取提示信息封装返回
        ResultCode rce = ResultCode.SYSTEM_EXECUTION_ERROR;
        return R.failed(rce,e.getMessage());
    }

    /**
     * 演示模式异常
     */
    @ExceptionHandler(DemoModeException.class)
    public R<?> handleDemoModeException(DemoModeException e) {
        return R.failed("【哒哒开发平台】\uD83D\uDCE2：UAT环境，仅供预览");
    }


}
